using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Navigation;
using MSIACCSL2.ViewModels;
using MSIACCSL2.Views.FormSearch;
using WcfService1.DTO;

namespace MSIACCSL2.Views.SalesReturn
{
    public partial class FrmAdd : Page
    {

        public SalesReturnViewModel VM
        {
            get
            {
                return this.DataContext as SalesReturnViewModel;
            }
        }

        public SalesReturnWrapViewModel WrapViewModel
        {
            get { return (this.Resources["POW"] as SalesReturnWrapViewModel); }
        }

        public FrmAdd()
        {
            InitializeComponent();

            this.Loaded += (x,y) =>
                               {
                                   (this.Resources["POW"] as SalesReturnWrapViewModel).SalesReturnViewModel =
                                        (SalesReturnViewModel)this.DataContext;
                               };

            this.Loaded += (o, e) =>
            {
                txtCustomer.Focus();
            };

       
            this.txtCustomer.KeyDown += (o, e) =>
            {
                if (e.Key == Key.Enter)
                {
                    //VM.GetCustomer(txtCustomer.Text);
                    CustomerSearch customerSearch = new CustomerSearch();
                    customerSearch.Closed += new EventHandler(customerSearch_Closed);
                    customerSearch.Show();
                    customerSearch.txtCustomerName.Text = txtCustomer.Text;
                }
            };

            this.KeyDown += (o, e) =>
            {

                if (e.Key == Key.S && Keyboard.Modifiers == ModifierKeys.Control)
                    VM.SaveSalesReturn(VM.SalesReturn);
                else if (e.Key == Key.C && Keyboard.Modifiers == ModifierKeys.Control)
                    VM.Cancel(null);
            };
        
        }

        void customerSearch_Closed(object sender, EventArgs e)
        {
            VM.SalesReturn.Customer = (sender as CustomerSearch).Customer;
            txtCustomer.Focus();
        }


        private void textBox_KeyDown(object sender, System.Windows.Input.KeyEventArgs e)
        {
            if (e.Key == Key.Enter)
                WrapViewModel.GetInventory(((TextBox)sender).DataContext);
        }

        private void cboCostCenter_SelectionChanged(object sender, System.Windows.Controls.SelectionChangedEventArgs e)
        {
            if (cboCostCenter.SelectedItem != null)
                VM.GetSalesInvoiceList(cboCostCenter.SelectedItem as CostCenterDTO);
            //if (String.IsNullOrEmpty(VM.SalesReturn.SRNo))
            //         VM.GetLastNoSalesReturn(cboCostCenter.SelectedValue);
        }

        private void btnCloseStatus_Click(object sender, System.Windows.RoutedEventArgs e)
        {
            VM.CloseStatus();
        }
     

    }
}

